#include "AppSegmenter.h"


#include "abliefern/PolygonPath.h"


using namespace abliefern;




//--------------------------------------------------------------
void AppSegmenter::setup(){
	
	
	
	_myInputPath = "/Volumes/Vektor/Images Klaus Tiff/TIFFS";
	
	_mySvgPath = "/Volumes/Vektor/Segmentierung/Run3.1"; // change me
	_myOutputSuffix = "1"; // change me

	
	_myOutputPath = "/Volumes/Vektor/Segments/Segments3";
	
	
	
	/*
	 _myInputPath = ofToDataPath("input");
	 _mySvgPath = ofToDataPath("svg");
	 _myOutputPath = ofToDataPath("output");
	 */ 
	
	
	/* DB */
	
	Databaser d;
	
	_myDatabaser.openDB();
	
	
	_myThreads = new SegmentOracle*[4];
	
	
	for(int i = 0; i < 4; i++){
		_myThreads[i] = NULL;
	}
	
	
	
	_myIAmFinished = false;
	
	
	
}

//--------------------------------------------------------------
void AppSegmenter::update(){
	
	
	
}


//--------------------------------------------------------------
void AppSegmenter::draw(){
	
	ofSetColor(128,128,128);
	
	
	ofSetColor(255,0,0);
	
	//ofRect(_myMinX * 0.3, _myMinY * 0.3, (_myMaxX - _myMinX) * 0.3, (_myMaxY - _myMinY) * 0.3);
	
	if(!_myIAmFinished){
		
		for(int i = 0; i < 4; i++){
			SegmentOracle* myOracle = _myThreads[i];
			
			if(myOracle){
				if(myOracle->hasFinishedCompletely()){
					std::vector<ImageSegment> mySegments = myOracle->getSegments();
					
					KlausImage* myImage = myOracle->getKlausImage();
					
					_myDatabaser.setImageState(myImage, Databaser::PICTURE_STATE_SEGMENTED); // prevent double processing
					
					for(int j = 0; j < mySegments.size(); j++){
						ImageSegment mySegment = mySegments.at(j);
						
						_myDatabaser.saveImageSegment(&mySegment, &mySegment.vanishingPoint);
						
					}
					
					
					delete myOracle;
					myOracle = NULL;
					
					_myThreads[i] = NULL;
				}
			} else {
				
				KlausImage* myImage = _myDatabaser.getImageWithState(Databaser::PICTURE_STATE_VANISHING_POINTED);
				
				if(myImage){
					
					_myDatabaser.setImageState(myImage, Databaser::PICTURE_STATE_SEGMENTING); // prevent double processing
					
					std::vector<VanishingPoint>* myVPs =  _myDatabaser.getVanishingPointsOfImage(myImage);
					
					
					std::cout << "### Has" << myVPs->size() << " Vanishing Points \n";
					
					if( myVPs->size() > 0){
						
						
						
						myOracle = new SegmentOracle(myImage, _myInputPath, _mySvgPath, _myOutputPath, _myOutputSuffix);
						
						for(int j = 0; j < myVPs->size(); j++){
							
							myOracle->addVanishingPoint(myVPs->at(j));
						}
						
						
						
						myOracle->start();
						
						
						_myThreads[i] = myOracle;
						
					} else {
						delete myImage;
					}
					
					delete myVPs;
					
				} else {
					
					
					_myIAmFinished = true;
					
					
					std::cout << "FINISHED\n";
					std::cout << "FINISHED\n";
					
					std::cout << "FINISHED\n";
					
					std::cout << "FINISHED\n";
					
					std::cout << "FINISHED\n";
					
					
					
				}
				
				//delete myImage;
				
				
				
				
				
			}
			
			
			
			
			
			
		}
		
	}
	
}





//--------------------------------------------------------------
void AppSegmenter::keyPressed  (int key){
	
}

//--------------------------------------------------------------
void AppSegmenter::keyReleased(int key){
	
}

//--------------------------------------------------------------
void AppSegmenter::mouseMoved(int x, int y ){
	
}

//--------------------------------------------------------------
void AppSegmenter::mouseDragged(int x, int y, int button){
	
}

//--------------------------------------------------------------
void AppSegmenter::mousePressed(int x, int y, int button){
	
}

//--------------------------------------------------------------
void AppSegmenter::mouseReleased(int x, int y, int button){
	
}

//--------------------------------------------------------------
void AppSegmenter::resized(int w, int h){
	
}













